package com.st.event.demo.scanner;

import com.st.event.demo.source.EventAttributeSource;
import com.st.event.demo.strategy.EventStrategy;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;

import java.util.Objects;
import java.util.Set;

public class EventListenerRegistryScanner extends EventListenerAbstractRegistryScanner {

    private ApplicationContext ac;
    private EventAttributeSource eventAttributeSource;
    private EventStrategy eventStrategy;

    public EventListenerRegistryScanner(BeanDefinitionRegistry registry) {
        super(registry);
    }

    @Override
    protected void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {
        GenericBeanDefinition definition;
        System.out.println("执行7-----------");
        for (BeanDefinitionHolder holder : beanDefinitions) {
            definition = (GenericBeanDefinition) holder.getBeanDefinition();
            definition.getConstructorArgumentValues().addGenericArgumentValue(Objects.requireNonNull(definition.getBeanClassName()));
            if (ac != null) {
                definition.getPropertyValues().add("ac", ac);
            }
            if (eventAttributeSource != null) {
                definition.getPropertyValues().add("eventAttributeSource", eventAttributeSource);
            }
            if (eventStrategy != null) {
                definition.getPropertyValues().add("eventStrategy", eventStrategy);
            }
            definition.setBeanClass(getFactoryBean().getClass());
            definition.setPrimary(true);
            definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
        }
    }

    public void setAc(ApplicationContext ac) {
        this.ac = ac;
    }

    public void setEventAttributeSource(EventAttributeSource eventAttributeSource) {
        this.eventAttributeSource = eventAttributeSource;
    }
}
